﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UCS.XNA.Utils;
using UCS.XNA.Utils.Geometries;

namespace DivideAndConquer.States.InGame
{
    public class Ball : ICircle
    {
        #region Private Members

        private Texture2D _texture;

        #endregion

        #region Attributes and Properties

        private float _radius;
        public float Radius
        {
            get { return _radius; }
        }

        private float _x;
        public float X
        {
            get { return _x; }
        }

        private float _y;
        public float Y
        {
            get { return _y; }
        }

        private float _scale = 1;
        public float Scale
        {
            get { return _scale; }
            set { _scale = value; }
        }

        private Vector2 _direction = new Vector2(1, 1);
        public Vector2 Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        private float _speedFactor = 2.5f;
        public float SpeedFactor
        {
            get { return _speedFactor; }
            set { _speedFactor = value; }
        }

        #endregion

        #region Contructors

        public Ball(Vector2 position)
        {
            _x = position.X;
            _y = position.Y;

            this.InitializeTexture();
        }

        #endregion

        #region Private Methods

        private void InitializeTexture()
        {
            _texture = GameResources.Content.Load<Texture2D>(@"Textures\Ball");

            this._radius = _texture.Width / 2;
        }

        private void Move()
        {
            Vector2 position = new Vector2(_x, _y);
            Vector2 newPosition = position + _direction * _speedFactor;

            _x = newPosition.X;
            _y = newPosition.Y;
        }

        #endregion

        #region Public Methods

        public void Update(GameTime gameTime)
        {
            this.Move();
        }

        public void Draw(GameTime gameTime)
        {
            GameResources.SpriteBatch.Begin();

            GameResources.SpriteBatch.Draw(texture: _texture,
                                           position: new Vector2((int)_x, (int)_y),
                                           sourceRectangle: null,
                                           color: Color.White,
                                           rotation: 0,
                                           origin: new Vector2((int)_radius, (int)_radius),
                                           scale: _scale,
                                           effects: SpriteEffects.None,
                                           layerDepth: 0);

            GameResources.SpriteBatch.End();
        }

        #endregion
    }
}